import advice.AppConfig;
import com.sun.org.apache.bcel.internal.generic.NEW;
import dao.UserDao;
import entity.Address;
import entity.Student;
import entity.User;
import entity.User1;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import service.UserService;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;


public class FactoryTestSpring {
    public static void main(String[] args) {
//        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("/spring-context.xml");
        AnnotationConfigApplicationContext context =
                new AnnotationConfigApplicationContext(AppConfig.class);
        //        获取工厂对象
//        UserDao userDAO = (UserDao) context.getBean("userDAO");
//        userDAO.deleteUser(2);
//
//        UserService userService = (UserService) context.getBean("userService");
//        userService.deleteUser(3);

        Address addr = (Address) context.getBean("addr");
        System.out.println("addr="+addr);
    }

    @Test
    public void testDbConnection()throws SQLException{
        AnnotationConfigApplicationContext context =
                new AnnotationConfigApplicationContext(AppConfig.class);
//        //1.启动工厂
//        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("/spring-context.xml");
//        获取工厂对象
        Connection conn = (Connection) context.getBean("conn");//核心
        PreparedStatement preparedStatement = conn.prepareStatement("select * from user");
        ResultSet resultSet = preparedStatement.executeQuery();
        System.out.println("resultSet.next()="+resultSet.next());
        System.out.println("resultSet.getString('username')="+resultSet.getString("username"));
    }

    @Test
    public void testUser()throws SQLException{
        AnnotationConfigApplicationContext context =
                new AnnotationConfigApplicationContext(AppConfig.class);
//        //1,启动工厂
//        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("/spring-context.xml");
        User1 user = (User1) context.getBean("user");//核心
        User1 user1 = (User1) context.getBean("user");//核心
        User1 user2 = (User1) context.getBean("user");//核心
        System.out.println(user);
        System.out.println(user1);
        System.out.println(user2);
    }


    @Test
    public void testStudent() throws SQLException{
        //1,启动工厂
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("/spring-context.xml");
        Student student = (Student) context.getBean("student");//核心
        System.out.println(student);
//        UserService userService = (UserService) context.getBean("userService");
//        userService.deleteUser(3);
    }

//    @Test
//    public void testMyBatis(){
//        //1,启动工厂
//        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("/spring-context.xml");
//        SqlSessionFactory sqlSessionFactory = (SqlSessionFactory) context.getBean("sqlSessionFactory");//核心
//        SqlSession sqlSession = sqlSessionFactory.openSession();
//        UserDao mapper = sqlSession.getMapper(UserDao.class);
//        List<User> users = mapper.queryStus();
//        for (User user : users) {
//            System.out.println(user);
//        }
//        sqlSession.close();
//    }

    @Test
    public void testaop() {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-context.xml");
        UserService userService = (UserService) context.getBean("userService");
        userService.deleteUser(3);
    }




}
